package com.canyou.enums;

import com.canyou.api.IErrorCode;
import lombok.Getter;
import lombok.ToString;

/**
 * api返回响应Code
 *
 * @author cgz
 * @date 2018-12-06
 */
@Getter
@ToString
public enum ErrorCodeEnum implements IErrorCode {

    /**
     * 系统常见错误枚举
     */
    UN_KNOWN(-10000, "未知错误"),
    SUCCESS(2000, "请求成功"),

    INVALID_ACCESS_TOKEN(1010, "access_token无效"),
    INVALID_REFRESH_TOKEN(1011, "refresh_token无效"),
    INVALID_GRANT(1012, "用户名或密码错误"),
    USER_LOCKED(1013, "用户被锁定"),

    UNAUTHORIZED(1401, "用户未授权"),
    ILLEGAL(1402, "参数非法"),
    NOTFOUND(1404, "URL错误"),

    FAIL(1500, "系统繁忙, 请稍后再试"),

    REPEAT_SUBMIT(1600, "重复提交"),

    FREQUENT_REQUESTS(1601, "请求太频繁"),

    VERSION_ERROR(1700, "您所编辑的数据已被其它用户编辑过了，请重新打开页面提交。"),

    DEL_DATA_USED(1800, "删除失败，数据已被使用"),

    SEND_SMS_ERROR(1301, "发送短信失败"),

    NON_LOCAL_REQUEST(1900, "非本域请求"),

    ;


    /**
     * 错误码枚举
     *
     * @param code 状态码
     * @param msg  提示信息
     */
    ErrorCodeEnum(final Integer code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    private String msg;
    private Integer code;

    /**
     * 根据 code 获取对应错误枚举
     *
     * @param code
     * @return
     */
    public static ErrorCodeEnum valueOf(Integer code) {
        ErrorCodeEnum[] enums = ErrorCodeEnum.values();
        for (ErrorCodeEnum errorCodeEnum : enums) {
            if (errorCodeEnum.getCode().equals(code)) {
                return errorCodeEnum;
            }
        }
        return ErrorCodeEnum.UN_KNOWN;
    }

}
